import { FakeApi, random } from '@wowon/utils';
import { getCurrentUser } from '../../auth-center';
import { roleList } from './role';
import { fileList } from '../system/file';

const nameArray = [
  '赵钱孙李周吴郑王张刘陈朱姜崔谭',
  '娟燕丽玉玲盼珠英颖盈莹梦莉雨语婷翠茹萍晴蕾馨娜美舒',
  '雷博伟强克国浩宇文星鑫鹏阳洋山海柯志龙杰旭磊树刚飞',
];

const PRESET_USERS = [
  { username: 'developer', enable: '1', roleKey: 'developer' },
  { username: 'admin', enable: '1', roleKey: 'admin' },
  { username: 'user', enable: '1', roleKey: 'user' },
  { username: 'guest', enable: '1', roleKey: 'guest' },
].map((item) => ({ ...item, roles: roleList.getItem({ key: item.roleKey }) }));

const initData = Array.from({ length: random(100, 200) }).map((_, i) => {
  const sex = random(['0', '1']);
  const name = Array(random(11) < 8 ? 3 : 2)
    .fill()
    .map((_, i) => random(nameArray[i === 0 ? 0 : +sex + 1].split('')))
    .join('');
  const role = roleList.pick();
  return {
    avatar: fileList.pick({ name: 'avatar' }),
    cover: fileList.pick({ name: 'cover' }),
    name: name,
    username: i === 0 ? 'admin' : `user-${i}`,
    password: '123456',
    passwordLevel: 0,
    phone: random(12000000000, 19999999999).toString(),
    email: null,
    wechat: null,
    sex,
    // roles: roleList
    //   .getList({ enable: '1' })
    //   .sort(() => 0.5 - Math.random())
    //   .slice(0, random(1, 4)),
    roles: role,
    roleKey: role.key,
    enable: i === 0 ? '1' : random(['0', '1']),
    ...(PRESET_USERS[i] || {}),
  };
});

export const userList = new FakeApi(initData, {
  queryType: {
    id: 'is',
    name: 'like',
    username: 'like',
    phone: 'like',
    sex: 'is',
    roleKey: 'is',
    enable: 'is',
  },
  sort: () => 0,
});

export default {
  'GET /mock/user/query': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await userList.query(req.query);
    res.json(result);
  },
  'POST /mock/user/create': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const { roleKey } = req.body;
    const result = await userList.create({ ...req.body, roles: roleList.getItem({ key: roleKey }) });
    res.json(result);
  },
  'POST /mock/user/update': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const { roleKey } = req.body;
    const result = await userList.update({ ...req.body, roles: roleList.getItem({ key: roleKey }) });
    res.json(result);
  },
  'POST /mock/user/remove': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await userList.remove(req.body);
    res.json(result);
  },
  'GET /mock/user/profile': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await userList.profile(req.query);
    res.json(result);
  },
};
